package org.ghost.util;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *
 * @author 2014-400
 */
public class ScreenUtil {

    /**
     * 居中显示
     *
     * @param frame
     */
    public static void setCenter(JFrame frame) {
        int windowWidth = frame.getWidth(); //获得窗口宽
        int windowHeight = frame.getHeight(); //获得窗口高
        Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
        Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
        int screenWidth = screenSize.width; //获取屏幕的宽
        int screenHeight = screenSize.height; //获取屏幕的高
        frame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);//设置窗口居中显示
    }

    /**
     * Frame的icon修改
     *
     * @param frame
     * @param src
     * @throws IOException
     */
    public static void setIcon(JFrame frame, String src) throws IOException {
        Image imgae = ImageIO.read(frame.getClass().getResource(src));
        frame.setIconImage(imgae);
    }
}
